package com.lz.football.module.friend
{
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.FriendEvent;
	import com.liangzhong.events.NotifierEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.chat.vo.ChatVo;
	import com.liangzhong.model.friend.vo.FriendInfoVo;
	import com.liangzhong.model.friend.vo.FriendVo;
	import com.lz.football.module.base.IComponent;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import lz.controls.Alert;

	/**
	 *好友浮动面板 
	 * @author Administrator
	 * 
	 */	
	public class DriftView extends Sprite implements IComponent
	{
		private var _skin:MovieClip;
		
		private var _friendVo:FriendVo;
		
		/**
		 *查看 
		 */
		private var friend_lookBtn:SimpleButton;
		private var friend_fightBtn:SimpleButton;
		/**
		 *私聊 
		 */
		private var friend_chatBtn:SimpleButton;
		/**
		 *删除 
		 */
		private var friend_deleteBtn:SimpleButton;
		/**
		 *添加  
		 */
		private var friend_addBtn:SimpleButton;
		private static var _instance:DriftView;
		
		public function DriftView()
		{
			super();
		}
		
		public function initUI(view:Object=null):void
		{
			if(view == null)
			{
				_skin = Reflection.createInstance("com.liangzhong.ui.driftview");
			}else if(view is String)
			{
				_skin = Reflection.createInstance(view as String);
			}
			if(_skin == null)return;
			friend_lookBtn = _skin.friend_lookBtn;
			friend_fightBtn = _skin.friend_fightBtn;
			friend_chatBtn = _skin.friend_chatBtn;
			friend_deleteBtn = _skin.friend_deleteBtn
			friend_addBtn = _skin.friend_addBtn;
			
			
			this.addChild(_skin);
			addEvtListener();
//			friend_lookBtn = _skin.friend_lookBtn;
//			friend_lookBtn
		}
		
		public function addEvtListener():void
		{
			this.addEventListener(MouseEvent.MOUSE_DOWN,onClickHandler);
			this.addEventListener(Event.ADDED_TO_STAGE,onAddToStageHandler);
		}
		
		protected function onAddToStageHandler(event:Event):void
		{
			if(this.stage)
				this.stage.addEventListener(MouseEvent.MOUSE_DOWN,onClickStageHandler);
//			friend_lookBtn.
		}
		
		protected function onClickStageHandler(event:MouseEvent):void
		{
			if(this.parent)
				this.parent.removeChild(this);
		}
		
		protected function onClickHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
//			trace(event.target.name);
//			if(this.parent)
//				this.parent.removeChild(this);
			var f:FriendInfoVo = new FriendInfoVo();
			f.friendid = _friendVo.friendid;
			
			switch(event.target.name)
			{
				case "friend_lookBtn"://查看
					AppEventDispatcher.getInstance().dispatchEvent(new FriendEvent(FriendEvent.LOOK_UP_FRIEND,{vo:f,type:1}));
					break;
				case "friend_fightBtn"://挑战
					f.isRequestInfo = true;
					AppEventDispatcher.getInstance().dispatchEvent(new FriendEvent(FriendEvent.LOOK_UP_FRIEND,{vo:f,type:2}));
					break;
				case "friend_chatBtn"://私聊
					if(_friendVo.friendid == AppData.getInstance().clubDatamanager.clubInfo.clubid)
					{
						Alert.showErrorTip("不可以同自己私聊");
						return;
					}
					var chatvo:ChatVo = new ChatVo();
					chatvo.name = _friendVo.uname;
					chatvo.id = _friendVo.friendid;
					AppEventDispatcher.getInstance().dispatchEvent(new NotifierEvent(NotifierEvent.PRIVATE_CHAT,{vo:chatvo}));
					break;
				case "friend_deleteBtn"://删除 
					Alert.show("你确定要将" +_friendVo.uname+ "移出你的好友名单吗？","",Alert.NO,GlobalDef.AlertContainer,deleteAlert);
					break;
				case "friend_addBtn"://添加 
					
					AppEventDispatcher.getInstance().dispatchEvent(new FriendEvent(FriendEvent.INVITE_FRIENDS,{array:[_friendVo.friendid]}));
					break;
			}
		}
		/**
		 *删除  
		 * @param $type
		 * 
		 */
		private function deleteAlert($type:int):void
		{
			if($type == Alert.YES)
			{
				AppEventDispatcher.getInstance().dispatchEvent(new FriendEvent(FriendEvent.DELETE_FRIEND,{id:_friendVo.friendid}));
			}
		}
		
		public function removeEvtListener():void
		{
			this.removeEventListener(MouseEvent.MOUSE_DOWN,onClickHandler);
			this.removeEventListener(Event.ADDED_TO_STAGE,onAddToStageHandler);
		}
		
		public function destroyView():void
		{
			removeEvtListener();
		}

		/**
		 * 
		 */
		public function get friendVo():FriendVo
		{
			return _friendVo;
		}

		/**
		 * @private
		 */
		public function set friendVo(value:FriendVo):void
		{
			_friendVo = value;
			var vo:FriendVo = AppData.getInstance().friendManager.getFriendById(_friendVo.friendid);
			friend_addBtn.visible = false;
			friend_deleteBtn.visible = false;
			if(vo == null)
			{
				friend_addBtn.visible = true;
			}else
			{
				friend_deleteBtn.visible = true;
			}
			GlobalDef.AlertContainer.addChild(this);
			this.x = this.stage.mouseX;
			this.y = this.stage.mouseY;
		}

		/**
		 * 
		 */
		public static function get instance():DriftView
		{
			if(_instance == null)
			{
				_instance = new DriftView();
				_instance.initUI();
			}
			return _instance;
		}

		/**
		 *显示 
		 * 
		 */
		public function showView():void
		{
			GlobalDef.GameContainer.addChild(this);
			this.x = this.stage.mouseX;
			this.y = this.stage.mouseY;
		}

	}
}